package com.so206.controller.system;

import com.github.pagehelper.PageInfo;
import com.so206.controller.pay.process.UserBalnesChange;
import com.so206.po.SysTrRecord;
import com.so206.po.SystemUser;
import com.so206.service.SysTranssferAccountService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 转账管理 + 转账详情
 */
@Controller
@RequestMapping("/admin/tr_a")
public class TransafeController {

    @Autowired
    private SysTranssferAccountService transsferAccountService;

    @Autowired
    private UserBalnesChange userBalnesChange;

    /**
     * 转账管理 - 列表
     */
    @RequiresPermissions("sys_tr_a_list")
    @RequestMapping("/list2")
    public String list2(@RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "20") Integer rows,
                        @RequestParam(defaultValue = "false") Boolean ajax,
                        Model model, Integer status, Long src_pid, Long dst_pid, String to_context) {
        PageInfo<SysTrRecord> bean = transsferAccountService.find_by_page(page, rows, status, src_pid, dst_pid, to_context);
        model.addAttribute("bean", bean);
        model.addAttribute("status", status);
        model.addAttribute("src_pid", src_pid);
        model.addAttribute("dst_pid", dst_pid);
        model.addAttribute("to_context", to_context);
        if (ajax) {
            return "admin/tr/list2::pageNode";
        } else {
            return "admin/tr/list2";
        }
    }

    /**
     * 查看转账详情 - 管理员
     */
    @RequiresPermissions("sys_tr_a_list")
    @RequestMapping("/view")
    public String view(Long id, Model model) {
        SysTrRecord model1 = transsferAccountService.find_by_id(id);
        model.addAttribute("model", model1);
        return "admin/tr/view";
    }

    /**
     * 删除转账
     */
    @RequiresPermissions("sys_tr_a_list")
    @RequestMapping("/delete")
    @ResponseBody
    public Map delete(Long id) {
        HashMap<Object, Object> map = new HashMap<>();
        SysTrRecord p_model = transsferAccountService.find_by_parent_id(id);
        transsferAccountService.delete_by_id(p_model.getId());
        transsferAccountService.delete_by_id(id);
        map.put("msg", "删除成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * 转账审核
     */
    @RequiresPermissions("sys_tr_a_list")
    @RequestMapping("/check_update")
    @ResponseBody
    public Map check_update(Integer status, Long id) {
        HashMap<Object, Object> map = new HashMap<>();
        SysTrRecord p_model = transsferAccountService.find_by_parent_id(id);
        SysTrRecord model = transsferAccountService.find_by_id(id);
        if (model.getStatus() != 2) {
            map.put("msg", "操作失败,主记录错误!");
            map.put("code", -1);
            return map;
        }
        if (p_model.getStatus() != 2) {
            map.put("msg", "操作失败,冗余记录错误!");
            map.put("code", -1);
            return map;
        }
        if (status == 1) {
            p_model.setStatus(1);
            transsferAccountService.updat_record(p_model);
            model.setStatus(1);
            transsferAccountService.updat_record(model);
            userBalnesChange.addUserBalnes(model.getDstId(), model.getMoney());
        } else {
            p_model.setStatus(0);
            transsferAccountService.updat_record(p_model);
            model.setStatus(0);
            transsferAccountService.updat_record(model);
            userBalnesChange.addUserBalnes(model.getSrcId(), model.getMoney());
        }
        map.put("msg", "操作成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * 转账详情 - 列表
     */
    @RequiresPermissions("user_tr_info")
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer rows,
                       @RequestParam(defaultValue = "false") Boolean ajax,
                       Model model, Integer status, HttpSession session, Long dst_pid, String to_context) {
        SystemUser userInfo = (SystemUser) session.getAttribute("userInfo");
        PageInfo<SysTrRecord> bean = transsferAccountService.find_by_page2(page, rows, status, userInfo.getId(), dst_pid, to_context);
        model.addAttribute("bean", bean);
        model.addAttribute("status", status);
        model.addAttribute("dst_pid", dst_pid);
        model.addAttribute("to_context", to_context);
        if (ajax) {
            return "admin/tr/list::pageNode";
        } else {
            return "admin/tr/list";
        }
    }

    /**
     * 查看转账详情 - 商户
     */
    @RequiresPermissions("user_tr_info")
    @RequestMapping("/view2")
    public String view2(Long id, Model model) {
        SysTrRecord model1 = transsferAccountService.find_by_id(id);
        model.addAttribute("model", model1);
        return "admin/tr/view2";
    }

}
